package io.coderyeah.org.controller;

import io.coderyeah.basic.annotation.LogAnnotation;
import io.coderyeah.basic.Result;
import io.coderyeah.basic.annotation.PreAuthorize;
import io.coderyeah.org.dto.DeptDTO;
import io.coderyeah.org.domain.Department;
import io.coderyeah.org.service.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;


@RestController
@RequestMapping("/dept")
@Api("部门接口类")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    /**
     * @param deptDTO 部门查询参数
     * @return Result
     */
    @PreAuthorize(name = "部门列表", sn = "department:list")
    @LogAnnotation(module = "部门模块", operate = "分页查询部门列表")
    @ApiOperation("查询部门列表")
    @PostMapping("/list")
    public Result list(@RequestBody(required = false) DeptDTO deptDTO) {
        return departmentService.list(deptDTO);
    }

    /**
     * @param id 部门id
     * @return result
     */
    @PreAuthorize(name = "获取部门", sn = "department:getById")
    @LogAnnotation(module = "部门模块", operate = "通过ID查询部门")
    @ApiOperation("通过ID查询部门")
    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Long id) {
        final Department dept = departmentService.getById(id);
        return Result.success(dept);
    }

    @PreAuthorize(name = "部门保存", sn = "department:save")
    @LogAnnotation(module = "部门模块", operate = "添加/修改部门")
    @ApiOperation("添加/修改部门")
    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody DeptDTO deptDTO) {
        return departmentService.saveOrUpdate(deptDTO);
    }

    @PreAuthorize(name = "部门删除", sn = "department:delete")
    @LogAnnotation(module = "部门模块", operate = "通过id删除部门")
    @ApiOperation("删除部门")
    @DeleteMapping("/del/{id}")
    public Result deleteById(@PathVariable("id") Long id) {
        try {
            departmentService.removeById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
        return Result.success("删除成功");
    }

    @PreAuthorize(name = "部门状态修改", sn = "department:editType")
    @LogAnnotation(module = "部门模块", operate = "修改部门状态是否禁用")
    @ApiOperation("修改状态")
    @PostMapping("/editType")
    public Result editType(@RequestBody DeptDTO deptDTO) {
        return departmentService.editType(deptDTO);
    }

    @PreAuthorize(name = "部门批量删除", sn = "department:patchDel")
    @LogAnnotation(module = "部门模块", operate = "批量删除")
    @ApiOperation("批量删除")
    @PatchMapping("/delByBatch")
    @Transactional
    public Result delByBatch(@RequestBody DeptDTO deptDTO) {
        System.out.println(Arrays.toString(deptDTO.getIds()));
        departmentService.removeByIds(Arrays.asList(deptDTO.getIds()));
        return Result.success("批量删除成功");
    }

    @PreAuthorize(name = "部门树", sn = "department:tree")
    @LogAnnotation(module = "部门模块", operate = "获取部门无限级树")
    @ApiOperation("部门无限级树")
    @GetMapping("/trees")
    public Result getDepartmentTrees() {
        return departmentService.getDepartmentTrees();
    }

}
